package com.ruoyi.report_jl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_jl.domain.bo.ReportJlCjlkzhhljlbBaDetailBo;
import com.ruoyi.report_jl.domain.vo.ReportJlCjlkzhhljlbBaDetailVo;
import com.ruoyi.report_jl.domain.ReportJlCjlkzhhljlbBaDetail;
import com.ruoyi.report_jl.mapper.ReportJlCjlkzhhljlbBaDetailMapper;
import com.ruoyi.report_jl.service.IReportJlCjlkzhhljlbBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 矿质混合料配合组成试验检测记录-详情Service业务层处理
 *
 * @author tll
 * @date 2024-10-15
 */
@RequiredArgsConstructor
@Service
public class ReportJlCjlkzhhljlbBaDetailServiceImpl implements IReportJlCjlkzhhljlbBaDetailService {

    private final ReportJlCjlkzhhljlbBaDetailMapper baseMapper;

    /**
     * 查询矿质混合料配合组成试验检测记录-详情
     */
    @Override
    @FIllBasicInfo
    public ReportJlCjlkzhhljlbBaDetailVo queryById(Long detailId){
        return baseMapper.selectVoById(detailId);
    }

    /**
     * 查询矿质混合料配合组成试验检测记录-详情列表
     */
    @Override
    public TableDataInfo<ReportJlCjlkzhhljlbBaDetailVo> queryPageList(ReportJlCjlkzhhljlbBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportJlCjlkzhhljlbBaDetail> lqw = buildQueryWrapper(bo);
        Page<ReportJlCjlkzhhljlbBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询矿质混合料配合组成试验检测记录-详情列表
     */
    @Override
    public List<ReportJlCjlkzhhljlbBaDetailVo> queryList(ReportJlCjlkzhhljlbBaDetailBo bo) {
        LambdaQueryWrapper<ReportJlCjlkzhhljlbBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportJlCjlkzhhljlbBaDetail> buildQueryWrapper(ReportJlCjlkzhhljlbBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportJlCjlkzhhljlbBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, ReportJlCjlkzhhljlbBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(StringUtils.isNotBlank(bo.getShaikcc()), ReportJlCjlkzhhljlbBaDetail::getShaikcc, bo.getShaikcc());
        lqw.eq(bo.getLeijsy1() != null, ReportJlCjlkzhhljlbBaDetail::getLeijsy1, bo.getLeijsy1());
        lqw.eq(bo.getLeijsy2() != null, ReportJlCjlkzhhljlbBaDetail::getLeijsy2, bo.getLeijsy2());
        lqw.eq(bo.getLeijsy3() != null, ReportJlCjlkzhhljlbBaDetail::getLeijsy3, bo.getLeijsy3());
        lqw.eq(bo.getHecjp() != null, ReportJlCjlkzhhljlbBaDetail::getHecjp, bo.getHecjp());
        lqw.eq(StringUtils.isNotBlank(bo.getGuifyq()), ReportJlCjlkzhhljlbBaDetail::getGuifyq, bo.getGuifyq());
        return lqw;
    }

    /**
     * 新增矿质混合料配合组成试验检测记录-详情
     */
    @Override
    public Boolean insertByBo(ReportJlCjlkzhhljlbBaDetailBo bo) {
        ReportJlCjlkzhhljlbBaDetail add = BeanUtil.toBean(bo, ReportJlCjlkzhhljlbBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDetailId(add.getDetailId());
        }
        return flag;
    }

    /**
     * 修改矿质混合料配合组成试验检测记录-详情
     */
    @Override
    public Boolean updateByBo(ReportJlCjlkzhhljlbBaDetailBo bo) {
        ReportJlCjlkzhhljlbBaDetail update = BeanUtil.toBean(bo, ReportJlCjlkzhhljlbBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportJlCjlkzhhljlbBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除矿质混合料配合组成试验检测记录-详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
